package ShareYourWay.functions;

import java.io.Serializable;


import java.util.ArrayList;
import java.util.Collection;


import java.util.Iterator;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;

import ShareYourWay.project.Meetup;
import ShareYourWay.project.University;
import ShareYourWay.project.User;
import ShareYourWay.project.Usermeetup;
import ShareYourWay.util.HibernateUtil;

public class App implements Serializable{
	
	SignInSession s = null;
	User user = null;
	

	/**
	 * Comentario de prueba
	 */
	private static final long serialVersionUID = 1L;
	public void addUser(User u) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.save(u);
		session.getTransaction().commit();
		System.out.println("OK -- Usuario añadido");
		session.close();
	}
	public void addMeetup(Meetup m) {
		
		s = SignInSession.get();
		user = s.getUser();
		
		//Obtenemos la ciudad de origen
		
		String[] arrayocity= m.getOrigin().split(",");
		String ocity=arrayocity[arrayocity.length-3];
		arrayocity= ocity.split(" ");
		
		String oocity="";
		
		for(int i=2;i<arrayocity.length;i++){
			
			oocity=oocity+" "+arrayocity[i];
			
		}	
		
		
		
		//Obtenemos la ciudad de destino
		String[] arraydcity= m.getDestination().split(",");
		String dcity=arraydcity[arraydcity.length-3];
		arraydcity= dcity.split(" ");		
		
		
		String ddcity="";
		
		for(int i=2;i<arraydcity.length;i++){
			
			ddcity=ddcity+" "+arraydcity[i];
			
		}
		
		
		
		
	
		
		Session session = HibernateUtil.getSessionFactory().openSession();		
		
		
		session.beginTransaction();
		
		m.setOcity(oocity);
		m.setDcity(ddcity);
		
		System.out.println("---------Ocity"+m.getOcity()+" Dcity:"+m.getDcity());
		System.out.println("sdsaddddddd"+m.getMdate()+"ferferferf"+m.getTimeMeet());
//		m.setTimeMeet("timeMeet");
//		m.setMdate("DATE");
		SQLQuery query = session.createSQLQuery("SELECT * FROM user where id = :user_id");
		query.addEntity(User.class);
		query.setParameter("user_id", user.getId());
		List result = query.list();
		System.out.println(result.size());
		for (Iterator <User> iterator = result.iterator();iterator.hasNext();){
			User us =  iterator.next();
			m.setUser(us);
		}
		
		session.save(m);
		session.getTransaction().commit();
		System.out.println("OK -- Usuario añadido");
		session.close();
	}
public User Login(User u) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		
		session.beginTransaction();
		
		SQLQuery query = session.createSQLQuery("SELECT * FROM user where mail = :mail");
		query.addEntity(User.class);
		query.setParameter("mail", u.getMail());
		List result = query.list();
		System.out.println(result.size());
		for (Iterator <User> iterator = result.iterator();iterator.hasNext();){
			User us =  iterator.next();
			if(us.getPassword().equals(u.getPassword()))
			{
				return us;
			}			
			
		}
		
		
		session.close();
		return null;
	}

public List<Meetup> listMeet(User user) {
	
	/*
	s = SignInSession.get();
	user=s.getUser();*/

	
	Session session = HibernateUtil.getSessionFactory().openSession();
	
	session.beginTransaction();
	
	 ArrayList<Meetup> listameets = new ArrayList<Meetup>();
	
	SQLQuery query = session.createSQLQuery("SELECT * FROM meetup where slots >0 and id_creator <>:iduser AND id_meetup NOT IN(SELECT id_meetup from usermeetup where id_user=:iduser)");
	int iduser2 = user.getId();
	query.setParameter("iduser",iduser2);
	
	query.addEntity(Meetup.class);
	
	List result = query.list();
	System.out.println(result.size());
	
	for (Iterator <Meetup> iterator = result.iterator();iterator.hasNext();){
		
		System.out.println("dentro");
		Meetup m =  iterator.next();
		
		
		System.out.println("m:"+m.getDestination());
		listameets.add(m);
		

	}
	
	session.close();
	return listameets;
	
	
	
}

public void updateslot(Integer id) {
	
	Session session = HibernateUtil.getSessionFactory().openSession();
	session.beginTransaction();
	Meetup meet = new Meetup();
	session.load(meet, id);
	int slot= meet.getSlots() -1;
	System.out.println(meet.getOrigin());
	meet.setSlots(slot);
	session.update(meet);
	session.getTransaction().commit();
	session.close();
	System.out.println(meet.getSlots());
			
}

public void insertusermeetup(Integer idmeet, User user) {
//	s = SignInSession.get();
//	user=s.getUser();
	System.out.println("estamos en insertarusermeetup");
	
	Session session = HibernateUtil.getSessionFactory().openSession();
	session.beginTransaction();
	
	Meetup meet = new Meetup();
	session.load(meet, idmeet);
	System.out.println(meet.getDestination());
	int iduser = user.getId();
	session.load(user, iduser);
	System.out.println(user.getName());
	
	Usermeetup usermeetup = new Usermeetup();
	usermeetup.setMeetup(meet);
	usermeetup.setUser(user);
	usermeetup.setState(true);
	System.out.println(usermeetup.getUser().getName());
	
	session.save(usermeetup);
	session.getTransaction().commit();
	System.out.println("OK -- Usermeetup creado");
	session.close();
			
}


public void hideUserMeetup(Integer idmeet, User user) {
//	s = SignInSession.get();
//	user=s.getUser();
	System.out.println("estamos en hideusermeetup");
	
	Session session = HibernateUtil.getSessionFactory().openSession();
	session.beginTransaction();
	
	Meetup meet = new Meetup();
	session.load(meet, idmeet);
	System.out.println(meet.getDestination());
	User user3 = new User();
	int iduser = user.getId();
	session.load(user3, iduser);
	System.out.println(user.getName());
	
	Usermeetup usermeetup = new Usermeetup();
	usermeetup.setMeetup(meet);
	usermeetup.setUser(user3);
	usermeetup.setState(false);
	System.out.println(usermeetup.getUser().getName());
	
	session.save(usermeetup);
	session.getTransaction().commit();
	System.out.println("OK -- Usermeetup oculto");
	session.close();
			
}

public void updateUser(Integer id) {
	
	Session session = HibernateUtil.getSessionFactory().openSession();
	session.beginTransaction();
	User user = new User();
	session.load(user, id);
	
	String name = user.getName();
	String surname = user.getSurname();
	String password = user.getPassword();
	String mail = user.getMail();
	String phone = user.getPhone();
	String city = user.getCity();
	
	user.setName(name);
	user.setSurname(surname);
	user.setPassword(password);
	user.setMail(mail);
	user.setPhone(phone);
	user.setCity(city);
	session.update(user);
	session.getTransaction().commit();
	session.close();			
}

public void updateProfile(User u) {
	
	Session session = HibernateUtil.getSessionFactory().openSession();
	session.beginTransaction();
	User user = new User();

	int id = u.getId();
	String name = u.getName();
	String surname = u.getSurname();
	String nick = u.getNick();
	String password = u.getPassword();
	University uni = u.getUniversity();
	String mail = u.getMail();
	Boolean c = u.isCar();
	String phone = u.getPhone();
	String city = u.getCity();
	String sex = u.getSex();
	
	user.setId(id);
	user.setName(name);
	user.setSurname(surname);
	user.setNick(nick);
	user.setCar(c);
	user.setSex(sex);
	user.setPassword(password);
	user.setUniversity(uni);
	user.setMail(mail);
	user.setPhone(phone);
	user.setCity(city);
	session.update(user);
	session.getTransaction().commit();
	session.close();			
}

public List<Meetup> ListMeetupOn(User user) {
//	s = SignInSession.get();
//	user=s.getUser();
	
	Session session = HibernateUtil.getSessionFactory().openSession();
	
	session.beginTransaction();
	
	 ArrayList<Meetup> listameets = new ArrayList<Meetup>();
	
	SQLQuery query = session.createSQLQuery("SELECT * FROM meetup where id_creator <>:iduser AND id_meetup  IN(SELECT id_meetup from usermeetup where id_user=:iduser and state=1)");
	int iduser2 = user.getId();
	query.setParameter("iduser",iduser2);
	
	query.addEntity(Meetup.class);
	
	List result = query.list();
	System.out.println(result.size());
	
	for (Iterator <Meetup> iterator = result.iterator();iterator.hasNext();){
		
		System.out.println("dentro");
		Meetup m =  iterator.next();
		
		
		System.out.println("m:"+m.getDestination());
		listameets.add(m);
		

	}
	
	session.close();
	return listameets;
	
	
	
}

public void usersInMyMeetup()//En pruebas, tiene que devolver una lista con los nombres y los meetup a los que se han inscrito para un creador
{
	s = SignInSession.get();
	user=s.getUser();
	
	Session session = HibernateUtil.getSessionFactory().openSession();
	
	session.beginTransaction();
	
	SQLQuery query = session.createSQLQuery("SELECT id_user,id_meetup from usermeetup where id_meetup IN (SELECT id_meetup from meetup where id_creator :idcreator) AND state =1");
	int iduser2 = user.getId();
	query.setParameter("idcreator",iduser2);
	List result = query.list();
	
	
	
	
	
	}

public List<Meetup> listMyMeet(User user) {
	
	/*
	s = SignInSession.get();
	user=s.getUser();*/

	
	Session session = HibernateUtil.getSessionFactory().openSession();
	
	session.beginTransaction();
	
	 ArrayList<Meetup> listameets = new ArrayList<Meetup>();
	
	SQLQuery query = session.createSQLQuery("SELECT * FROM meetup where id_creator =:iduser");
	int iduser2 = user.getId();
	query.setParameter("iduser",iduser2);
	
	query.addEntity(Meetup.class);
	
	List result = query.list();
	System.out.println(result.size());
	
	for (Iterator <Meetup> iterator = result.iterator();iterator.hasNext();){
		
		System.out.println("dentro");
		Meetup m =  iterator.next();
		
		
		System.out.println("m:"+m.getDestination());
		listameets.add(m);
		

	}
	
	session.close();
	return listameets;
	
	}

public List<University> listUniversity() {
		
	Session session = HibernateUtil.getSessionFactory().openSession();
	session.beginTransaction();
	
	 ArrayList<University> list = new ArrayList<University>();
	
	SQLQuery query = session.createSQLQuery("SELECT * FROM university");
	
	query.addEntity(University.class);
	
	List result = query.list();
	System.out.println("universities. " + result.size());
	
	for (Iterator<University> iterator = result.iterator(); iterator.hasNext();){	
		System.out.println("dentro");
		University u =  iterator.next();
		System.out.println("u: "+ u.getName());
		list.add(u);
	}
	
	session.close();
	return list;
	}

public University getUniversity(int iduni) {
	
	Session session = HibernateUtil.getSessionFactory().openSession();
	session.beginTransaction();
	
	 University uni = new University();
	
	SQLQuery query = session.createSQLQuery("SELECT * FROM university where id_university=:iduni");
	query.setParameter("iduni",iduni);
	query.addEntity(University.class);
	
	List result = query.list();
	System.out.println("universities. " + result.size());
	
	for (Iterator<University> iterator = result.iterator(); iterator.hasNext();){	
		System.out.println("dentro");
		uni =  iterator.next();
		System.out.println("u: "+ uni.getName());
	}
	
	session.close();
	return uni;
	}

public University getUniversityByName(String name) {
	
	Session session = HibernateUtil.getSessionFactory().openSession();
	session.beginTransaction();
	
	 University uni = new University();
	
	SQLQuery query = session.createSQLQuery("SELECT * FROM university where name=:name");
	query.setParameter("name", name);
	query.addEntity(University.class);
	
	List result = query.list();
	System.out.println("universities. " + result.size());
	
	for (Iterator<University> iterator = result.iterator(); iterator.hasNext();){	
		System.out.println("dentro");
		uni =  iterator.next();
		System.out.println("u: "+ uni.getName());
	}
	
	session.close();
	return uni;
	}

public User getMeetupCreator (int idCreator) {
	
	Session session = HibernateUtil.getSessionFactory().openSession();
	session.beginTransaction();
	
	 User u = new User();
	
	SQLQuery query = session.createSQLQuery("SELECT * FROM user where id=:idCreator");
	query.setParameter("idCreator", idCreator);
	query.addEntity(User.class);
	
	List result = query.list();
	System.out.println("user. " + result.size());
	
	for (Iterator<User> iterator = result.iterator(); iterator.hasNext();){	
		System.out.println("dentro");
		user =  iterator.next();
		System.out.println("u: "+ user.getName());
	}
	
	session.close();
	return user;
}

public List<Meetup> listMeetbyOD(String origin, String destination,User user) {
	
	
	
	Session session = HibernateUtil.getSessionFactory().openSession();
	
	session.beginTransaction();
	
	List result = null;
	ArrayList<Meetup> listameets = new ArrayList<Meetup>();
	
	
	if(origin.equals("All") && destination.equals("All")){
		
		 SQLQuery query = session.createSQLQuery("SELECT * FROM meetup where slots >0 and id_creator <>:iduser AND id_meetup NOT IN(SELECT id_meetup from usermeetup where id_user=:iduser)");
			int iduser2 = user.getId();
			query.setParameter("iduser",iduser2);
			query.addEntity(Meetup.class);
			result = query.list();
			
		
	}	
	
	else {
	 
	 if(origin.equals("All")){
		 
		 SQLQuery query = session.createSQLQuery("SELECT * FROM meetup where slots >0 and id_creator <>:iduser AND id_meetup IN(SELECT id_meetup from meetup where dcity=:destination) AND id_meetup NOT IN(SELECT id_meetup from usermeetup where id_user=:iduser)");
			int iduser2 = user.getId();
			query.setParameter("iduser",iduser2);
			//query.setParameter("origin",origin);
			query.setParameter("destination",destination);
			query.addEntity(Meetup.class);
			result = query.list();
		 
	 }	
	 
	 if(destination.equals("All")){
		 
		 SQLQuery query = session.createSQLQuery("SELECT * FROM meetup where slots >0 and id_creator <>:iduser AND id_meetup IN(SELECT id_meetup from meetup where ocity=:origin) AND id_meetup NOT IN(SELECT id_meetup from usermeetup where id_user=:iduser)");
			int iduser2 = user.getId();
			query.setParameter("iduser",iduser2);
			query.setParameter("origin",origin);			
			query.addEntity(Meetup.class);
			result = query.list();		 
	 }
	 
	 else {
		 
		 SQLQuery query = session.createSQLQuery("SELECT * FROM meetup where slots >0 and id_creator <>:iduser AND id_meetup IN(SELECT id_meetup from meetup where ocity=:origin and dcity=:destination ) AND id_meetup NOT IN(SELECT id_meetup from usermeetup where id_user=:iduser)");
			int iduser2 = user.getId();
			query.setParameter("iduser",iduser2);
			query.setParameter("origin",origin);
			query.setParameter("destination",destination);
			query.addEntity(Meetup.class);
			result = query.list();	
		 
		 
	 }
	 
	
}
	
	for (Iterator <Meetup> iterator = result.iterator();iterator.hasNext();){
		
		System.out.println("dentro");
		Meetup m =  iterator.next();
		
		
		System.out.println("m:"+m.getDestination());
		listameets.add(m);
		

	}
	
	session.close();
	return listameets;
	
	
	
}



public List<Meetup> listMeetbyDestination(String destination,User user) {
	
	/*
	s = SignInSession.get();
	user=s.getUser();*/

	
	Session session = HibernateUtil.getSessionFactory().openSession();
	
	session.beginTransaction();
	
	 ArrayList<Meetup> listameets = new ArrayList<Meetup>();
	
	SQLQuery query = session.createSQLQuery("SELECT * FROM meetup where slots >0 and id_creator <>:iduser AND id_meetup IN(SELECT id_meetup from meetup where destination=:destination)");
	int iduser2 = user.getId();
	query.setParameter("destination",destination);
	query.setParameter("iduser",iduser2);
	
	query.addEntity(Meetup.class);
	
	List result = query.list();
	System.out.println(result.size());
	
	for (Iterator <Meetup> iterator = result.iterator();iterator.hasNext();){
		
		System.out.println("dentro");
		Meetup m =  iterator.next();
		
		
		System.out.println("m:"+m.getDestination());
		listameets.add(m);
		

	}
	
	session.close();
	return listameets;
	
	
	
}


public List<Meetup> listMeetbyVehicle(String vehicle,User user) {
	
	/*
	s = SignInSession.get();
	user=s.getUser();*/

	
	Session session = HibernateUtil.getSessionFactory().openSession();
	
	session.beginTransaction();
	
	 ArrayList<Meetup> listameets = new ArrayList<Meetup>();
	
	SQLQuery query = session.createSQLQuery("SELECT * FROM meetup where slots >0 and id_creator <>:iduser AND id_meetup IN(SELECT id_meetup from meetup where vehicle=:vehicle)");
	int iduser2 = user.getId();
	query.setParameter("iduser",iduser2);
	query.setParameter("vehicle",vehicle);
	
	query.addEntity(Meetup.class);
	
	List result = query.list();
	System.out.println(result.size());
	
	for (Iterator <Meetup> iterator = result.iterator();iterator.hasNext();){
		
		System.out.println("dentro");
		Meetup m =  iterator.next();
		
		
		System.out.println("m:"+m.getDestination());
		listameets.add(m);
		

	}
	
	session.close();
	return listameets;
	
	
	
}

public List<String> listOrigin() {
	
	Session session = HibernateUtil.getSessionFactory().openSession();
	
	session.beginTransaction();	
		
	
	SQLQuery query = session.createSQLQuery("SELECT DISTINCT ocity FROM meetup");
	
	
	List result = query.list();
	
	List<String> arraycitys = new ArrayList<String>();
	
	arraycitys.add("All");
	
	for(int i=0;i<result.size();i++){
		
			arraycitys.add((String) result.get(i));
			System.out.println("Ciudadades: "+result.get(i));
					
		
	}	
	
	session.close();
	return arraycitys;
	
	
	
}

public List<String> listdestination() {
	
	Session session = HibernateUtil.getSessionFactory().openSession();
	
	session.beginTransaction();
	
	 List<String> arraycitys = new ArrayList<String>();
	
	SQLQuery query = session.createSQLQuery("SELECT DISTINCT dcity FROM meetup");
	
	
	List result = query.list();
	
	arraycitys.add("All");
	
	for(int i=0;i<result.size();i++){
		

		arraycitys.add((String) result.get(i));
		System.out.println("Ciudadades: "+result.get(i));
				
		
	}	
	
	session.close();
	return arraycitys;
	
	
	
}

public List<String> listUniNames() {
	
	Session session = HibernateUtil.getSessionFactory().openSession();
	
	session.beginTransaction();
	
	String[] arraycitys;	
	
	SQLQuery query = session.createSQLQuery("SELECT DISTINCT name FROM university");
	
	
	List result = query.list();
	System.out.println(result.size());
	
	
	for(int i=0;i<result.size();i++){
		
		System.out.println("Universidades: "+result.get(i));
		
		
	}	
	session.close();
	return result;
	
	
	
}

}
